package com.tutorial.graphics;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingDoubleBuff extends JFrame{

	public SwingDoubleBuff(){
		init();
		this.setVisible(true);
	}
	
	public void init(){
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setBounds(0, 0, 400, 400);
		this.setResizable(false);
		Container pane = this.getContentPane();
		
		MyPanel3 panel = new MyPanel3();
		pane.add(panel,BorderLayout.CENTER);
		new Thread(panel).start();
	}
	
	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		
		new SwingDoubleBuff();
	}
}

class MyPanel3 extends JPanel implements Runnable{
	public int ypos = 0;
	public int xpos = 160;
	public double Angle = 50;
	
	@Override
	protected void paintComponent(Graphics arg0) {
		super.paintComponent(arg0);
		Graphics2D g = (Graphics2D) arg0;
		
//		g.setColor(Color.red);
//		g.fill3DRect(xpos, ypos, 100, 50, true);

		drawImg(g);
	}
	
	public void drawImg(Graphics2D g){
		Toolkit tk = Toolkit.getDefaultToolkit();
		Image img = tk.getImage("img/refresh.png");
		int w = img.getWidth(null);
		int h = img.getHeight(null);
		
		int x= (this.getWidth()-w)/2;
		int y= (this.getHeight()-h)/2;
		
		AffineTransform trans = new AffineTransform();
		double pi = Math.PI;
		trans.rotate(Angle * pi / 180.0, img.getWidth(null)/2, img.getHeight(null)/2);
		g.drawImage(img, trans,	null );
	}
	
	public void run() {
		try {
			while (true) {
				Thread.sleep(10);
				Angle = Angle + 2;
				if (Angle > 360) {
					Angle = 0;
				}
				this.repaint();
			}
		} catch (Exception e) {
		}
	}
	
}
